package com.exonline.back.controller.order;

import com.exonline.commons.entity.Courses;
import com.exonline.commons.entity.Order;
import com.exonline.commons.base.IBaseService;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @desc 订单管理M层
 * @author hbrj
 */
public class OrderService implements IBaseService {
IDao<Order> dao = new DaoImpl<>();

    /**
     * @desc 1.获取全部订单详细信息
     * @author hbrj
     */
    @Override
    public List queryAll(int pageSize, int pageNo) throws SQLException {
        //1.1查询全部订单
        List<Order> orders = dao.queryWithPagingAndConditions("SELECT * FROM orders ORDER BY createtime DESC", null, pageNo, pageSize, new BeanListHandler<>(Order.class));
        //1.2查询订单所属课程的详细信息
        for (Order order : orders) {
            String cid = order.getCid();
            Courses courses = dao.queryForObject("SELECT * FROM courses WHERE id = ?", new BeanHandler<>(Courses.class), cid);
            order.setCourse(courses);//1.3将订单所属课程信息存入订单对象(order)
        }
        return orders;
    }
    /**
     * @desc 2.条件查询指定订单信息
     * @author hbrj
     */
    @Override
    public Object queryOne(String field) throws SQLException {
        //1.创建两个list集合,分别用来装全部订单与根据条件筛选后的订单
        List<Order> tarOrder = new ArrayList<>();
        List<Order> orders = dao.queryWithPagingAndConditions("SELECT * FROM orders", null, 1, 1000, new BeanListHandler<>(Order.class));
        //2.查询订单所属课程的详细信息
        for (Order order : orders) {
            String cid = order.getCid();
            //2.1 根据id查询课程表获取到指定的课程信息
            Courses courses = dao.queryForObject("SELECT * FROM courses WHERE id = ?", new BeanHandler<>(Courses.class), cid);
            order.setCourse(courses);
        }
        for (Order order : orders) {
            if (order.getCourse().getCname().startsWith(field)) {
                tarOrder.add(order);//2.2将订单所属课程信息存入订单对象(order)
            }
        }
        return tarOrder;
    }
    /**
     * @desc 3.订单状态进行条件查询
     * @author hbrj
     */
    public List queryAll(String status,int pageSize, int pageNo) throws SQLException {
        //1.根据状态查询订单信息
        List<Order> orders = dao.queryWithPagingAndConditions("SELECT * FROM orders WHERE status = ? order by createtime desc", null, pageNo, pageSize, new BeanListHandler<>(Order.class),status);
        //2.查询指定状态的订单的课程信息
        for (Order order : orders) {
            String cid = order.getCid();
            //2.1根据id查询课程信息
            Courses courses = dao.queryForObject("SELECT * FROM courses WHERE id = ?", new BeanHandler<>(Courses.class), cid);
            order.setCourse(courses);//2.2将订单所属课程信息存入订单对象(order)
        }
        return orders;
    }

    public String getRowCount(String sql) throws SQLException {
        int rowCount = dao.queryCount(sql);
        return String.valueOf(rowCount);
    }
}
